package com.bishe.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bishe.config.DataException;
import com.bishe.pojo.UserBill;
import com.bishe.pojo.query.IndexQuery;
import com.bishe.pojo.response.Result;
import com.bishe.service.IndexService;
import com.bishe.service.UserBillService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IndexServiceImpl implements IndexService {

    @Resource
    private UserBillService userBillService;


    @Override
    public Map<String, Object> getIndexData(IndexQuery query) {
        StpUtil.checkLogin();
        List<UserBill> userBill = userBillService.list(
                Wrappers.lambdaQuery(UserBill.class)
                        .eq(UserBill::getUid, StpUtil.getLoginIdAsString())
                        .between(StrUtil.isAllNotBlank(query.getStartTime(), query.getEndTime()),
                                UserBill::getBillDate, query.getStartTime(), query.getEndTime())
                        .orderByDesc(UserBill::getBillDate)
        );

        if (CollUtil.isEmpty(userBill)) {
            throw new DataException("该用户暂无账单数据。。。");
        }

        DecimalFormat decimal = new DecimalFormat("#0.00");
        decimal.setRoundingMode(RoundingMode.DOWN);

        // 每个类型总支出
        Map<String, Object> typeSum = new HashMap<>();
        Map<String, List<UserBill>> typeGroup = userBill.stream().collect(Collectors.groupingBy(UserBill::getType));
        typeGroup.forEach((k, v) ->{
            if (CollUtil.isNotEmpty(v)){
                BigDecimal typePay = v.stream()
                        .filter(s -> s.getPm() == 1)
                        .map(UserBill::getNumber)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                typeSum.put(k, NumberUtil.decimalFormat("#0.00",typePay));
            }
        });

        // 总收入 pm=0 收入
        BigDecimal income = userBill.stream()
                .filter(s -> ObjectUtil.isNotEmpty(s.getNumber()) && s.getPm() == 0)
                .map(UserBill::getNumber)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        // 总支出 pm=1 支出
        BigDecimal pay = userBill.stream()
                .filter(s -> ObjectUtil.isNotEmpty(s.getNumber()) && s.getPm() == 1)
                .map(UserBill::getNumber)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        // 大宗消费
        List<BigDecimal> bigPayList = userBill.stream()
                .filter(s -> s.getPm() == 1 && s.getNumber().compareTo(BigDecimal.valueOf(1000)) > 0)
                .map(UserBill::getNumber).collect(Collectors.toList());

        BigDecimal bigPay = bigPayList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);


        Map<String, Object> result = new HashMap<>();
        result.put("income", NumberUtil.decimalFormat("#0.00",income));
        result.put("pay", NumberUtil.decimalFormat("#0.00",pay));
        result.put("bigPay", NumberUtil.decimalFormat("#0.00",bigPay));
        result.put("bigPaySize", bigPayList.size());
        result.put("typeSum", typeSum);
        return result;
    }
}
